<?php

class pro {

 	private $error;
	private $url;

	function __construct($url) {

		/*
		 * load connection class
		 */
		include_once $url . 'includes/conn.php';

		/*
		 * load error report class
		 */
		include_once $url . 'admin/class/error.class.php';
		$error = new Errmsg();
		$this->set_error($error);
		$this->set_url($url);

	}

	/*
	 * defin the function for products add
	 *
	 * $fun=1 means add
	 * $fun=0 means update
	 *
	 */

	function products_add($id, $sort_id, $image, $name, $introduction, $fun) {
		$conn = new conn();

		$error = 'null';



			if ($fun) {
				/*
				 * add products sql
				 *
				 */
				$sql = sprintf("INSERT INTO products (id,sort_id,name,image,introduction,date)VALUES(null,'%d','%s','%s','%s',NOW())", $sort_id, $name, $image, $introduction);
				$error = 'add products error';

			}
			else{
				/*
				* update products sql
				*/
				$sql = sprintf("update products set sort_id='%d',name='%s',image='%s',introduction='%s' where id='%d' ", $sort_id, $name, $image, $introduction, $id);

				$error = 'update products error';


			}


			$conn->set_sql($sql);

			$line = $conn->get_line();

			return $line;



	}
	/*
	 * define products_get function
	 *
	 * $id is the id of the data which you need
	 *
	 * if $id is array , the function will get result as array
	 *
	 *    select with all or limit
	 *
	 */

	function products_get($id) {

		$conn =new conn();
		$url = $this->get_url();
		include_once $url . 'includes/entity/product.entity.php';

				if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from products limit %d,%d ", $a, $b);

		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from products where id='%d' ", $id);

		}

		$conn->set_sql($sql);

		$line = $conn->get_line();

		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$products[$i] = new product();
				$id = $row['id'];
				$sort_id = $row['sort_id'];
				$image = $row['image'];
				$name = $row['name'];
				$introduction = $row['introduction'];
				$date = $row['date'];
				$products[$i]->set_id($id);
				$products[$i]->set_sort_id($sort_id);
				$products[$i]->set_images($image);
				$products[$i]->set_name($name);
				$products[$i]->set_introduction($introduction);
				$products[$i]->set_date($date);
							}

		} else {
			$info= 'result null';
			$this->set_error($info);
			$products = null;
		}
		return $products;
	}

	/*
	 *   defin products delete function
	 *
	 *   delete products with id or ids
	 */

	function products_del($ids) {

		if (isset ($ids)) {

			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i<$max; $i++) {
					$conn = new conn();

					$sql = sprintf("delete from products where id=%d ; ",$ids[$i]);

				    $conn->set_sql($sql);

			        $line1 = $conn->get_line();

				    $line+=$line1;
				}

			} else {
				$conn = new conn();
				$sql = sprintf("delete from products where id=%d", $ids);

			$conn->set_sql($sql);

			$line = $conn->get_line();
			}


		} else {
			$info='at least choose one for process please';
			$line = 0;

			$this->set_error($info);

		}
		return $line;
	}

	/*
	 * functions for use above
	 */

	function set_error($error) {
		$this->error = $error;
	}
	function get_error() {
		return $this->error;
	}

	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}

}
?>